﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShare;
using EFong.View.Proxy;

namespace EFong.View.Customer
{
    public partial class Frm_SetWebUser : BasicForm
    {
        private Proxy.MasterServiceProxy _masterProxy {get{ return Proxy.MasterServiceProxy.GetInstance();}}
        private Proxy.BaseServiceProxy _baseProxy {get{ return Proxy.BaseServiceProxy.GetInstance();}}

        public Frm_SetWebUser()
        {
            InitializeComponent();
        }

        protected override void InitFields()
        {
        }

        protected override void Events()
        {
            this.btn_OK.Click += (s, e) =>
                {
                    this.save();
                };
        }

        protected override void LoadDatas()
        {
            // get availabel web users
            this.cmb_AvailableWebUser.Properties.BindingData(_baseProxy.GetAvailableWebUserList().ReturnValue);
        }

        private void save()
        {
            Contract.WebUser webUser = null;

            if (rad_New.Checked)
            {
                webUser = getWebUser();

                if (txt_Password.GetText() != txt_PasswordConfirm.GetText())
                {
                    MsgBox.Info("密码确认 与 密码 输入不一致.");
                    return;
                }

                CurWebU = webUser;
                DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                webUser = this.cmb_AvailableWebUser.GetValue<Contract.WebUser>();

                if (webUser == null)
                {
                    MsgBox.Info("请选择可用账户.");
                    return;
                }

                CurWebU = webUser;
                DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        // current web user
        private Contract.WebUser _curWebU;
        public Contract.WebUser CurWebU
        {
            get { return _curWebU; }
            set
            {
                _curWebU = value;
                onCurWebUChanged();
            }
        }

        private void onCurWebUChanged()
        {
            if (CurWebU == null)
            {
                this.rad_New.Checked = true;
                ControlExtensions.ClearValue(
                    this.txt_WenUserName,
                    this.txt_Password,
                    this.txt_PasswordConfirm,
                    this.cmb_AvailableWebUser
                    );
            }
            else
            {
                this.cmb_AvailableWebUser.SetValue(CurWebU);

                if (this.cmb_AvailableWebUser.GetValue() == null)
                {
                    txt_WenUserName.SetText(CurWebU.Name);
                    txt_Password.SetText(CurWebU.Password);
                    txt_PasswordConfirm.SetText(CurWebU.Password);
                    this.rad_New.Checked = true;
                }
                else
                {
                    this.rad_Old.Checked = true;
                }
            }
        }

        private Contract.WebUser getWebUser()
        {
            return new Contract.WebUser
            {
                Name = txt_WenUserName.GetText(),
                Password = txt_Password.GetText()
            };
        }
    }
}
